package com.kavenka.zhufengfm.app.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.kavenka.zhufengfm.app.R;
import com.kavenka.zhufengfm.app.cache.ImageCache;
import com.kavenka.zhufengfm.app.modle.DiscoverCategory;
import com.kavenka.zhufengfm.app.tasks.discover.ImageLoadTask;

import java.util.List;

/**
 * Created by kavenka on 2015/9/26 0026.
 */
public class DiscoverCategoryAdapter extends BaseAdapter {
    private Context context;
    private List<DiscoverCategory> categoryList;

    public DiscoverCategoryAdapter(Context context, List<DiscoverCategory> categories) {
        this.context = context;
        this.categoryList = categories;
    }

    @Override
    public int getCount() {
        int count=0;
        if (categoryList != null) {
            count = categoryList.size();
            count=count/2+(count%2);//实际的行数
        }
        return count;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view=null;
        if (convertView != null) {
            view=convertView;
        }else {
            view = LayoutInflater.from(context).inflate(R.layout.item_category_listview, parent, false);
        }
        CategoryViewHolder holder= (CategoryViewHolder) view.getTag();
        if (holder == null) {
            holder=new CategoryViewHolder();
            holder.txtCategory1= (TextView) view.findViewById(R.id.item_category_txt1);
            holder.txtCategory2= (TextView) view.findViewById(R.id.item_category_txt2);

            holder.icon1 = (ImageView) view.findViewById(R.id.item_category_icon1);
            holder.icon2 = (ImageView) view.findViewById(R.id.item_category_icon2);
            view.setTag(holder);
        }
        //item在集合中的位置
        int categoryIndex1=position*2;
        int categoryIndex2=categoryIndex1+1;
        //获取每个category
        DiscoverCategory category1=categoryList.get(categoryIndex1);
        DiscoverCategory category2=null;
        int size = categoryList.size();
        if (size>categoryIndex2){
            category2=categoryList.get(categoryIndex2);
        }

        //设置一个item中的category1的数据
        holder.txtCategory1.setText(category1.getTitle());
        String iconPath1 = category1.getCoverPath();
        holder.icon1.setTag(iconPath1);//给icon设置Tag，解决图片错位问题
        ImageCache imageCache=ImageCache.getInstance();
        Bitmap bitmap = imageCache.get(iconPath1);
        if (bitmap != null) {
            holder.icon1.setImageBitmap(bitmap);
        }else {
            //如果缓存中没有图片数据，进入异步任务下载
            holder.icon1.setImageResource(R.mipmap.ic_launcher);
            if (iconPath1 != null) {
                ImageLoadTask task=new ImageLoadTask(holder.icon1,context);
                task.execute(iconPath1);
            }
        }

        //设置一个item中的category2的数据
        holder.txtCategory2.setVisibility(View.VISIBLE);
        holder.icon2.setVisibility(View.VISIBLE);
        if (category2!=null){
            holder.txtCategory2.setText(category2.getTitle());
            String iconPath2=category2.getCoverPath();
            bitmap = imageCache.get(iconPath2);
            if (bitmap != null) {
                holder.icon2.setImageBitmap(bitmap);
            }else {
            holder.icon2.setTag(iconPath2);
                ImageLoadTask task=new ImageLoadTask(holder.icon2,context);
                task.execute(iconPath2);
            }
        }else {
            holder.txtCategory2.setVisibility(View.INVISIBLE);
            holder.icon2.setVisibility(View.INVISIBLE);
        }
        return view;
    }
    private static class CategoryViewHolder{
        public TextView txtCategory1;
        public TextView txtCategory2;
        public ImageView icon1;
        public ImageView icon2;
    }
}
